//
//  GYCancelOrderAlertView.m
//  GYDemo
//
//  Created by mac on 2018/4/17.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYCancelOrderAlertView.h"
#import "Head.h"

@interface GYCancelOrderSelectCell : UITableViewCell
@property (strong, nonatomic) UIImageView *titleImgV;
@property (strong, nonatomic) UILabel *titleLab;

@end
@implementation GYCancelOrderSelectCell
#pragma mark - getter and setter
-(UIImageView *)titleImgV
{
    if (!_titleImgV) {
        _titleImgV = [UIImageView new];
        _titleImgV.frame = WFCGRectMake(13, 14.5, 16, 16);
    }
    return _titleImgV;
}
-(UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.frame = WFCGRectMake(39, 14.5, WFCGFloatX(270), 16);
        _titleLab.font = SYSTEMFONT(13);
        _titleLab.textColor = Main_Color_Black;
    }
    return _titleLab;
}
#pragma mark - life
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.titleImgV];
        [self.contentView addSubview:self.titleLab];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

#define kSelectedTag     1
#define kUnSelectedTag   0

@interface GYCancelOrderAlertView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *subTitleLab;
@property (nonatomic, strong) GCBaseTableView *mTableView;
@property (nonatomic, strong) UIControl *overlayView;//背景
@property (nonatomic, strong) UIButton *submitBtn;
@property (nonatomic, strong) NSArray *titleAry;
@property (nonatomic, strong) NSMutableDictionary *selectDic;
@property (nonatomic,strong) NSMutableArray *selectSource;

@end

@implementation GYCancelOrderAlertView
#pragma mark - getter and setter
-(UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = [UIFont systemFontOfSize:15];
        _titleLab.textColor = Main_Color_Black;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = @"取消订单";
    }
    return _titleLab;
}
-(UIView *)line
{
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _line;
}
-(UILabel *)subTitleLab
{
    if (!_subTitleLab) {
        _subTitleLab = [[UILabel alloc] init];
        _subTitleLab.font = [UIFont systemFontOfSize:13];
        _subTitleLab.textColor = Main_Color_Black;
        _subTitleLab.text = @"请选择取消订单的原因:";
    }
    return _subTitleLab;
}

-(UIButton *)submitBtn
{
    @WeakSelf;
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _submitBtn.backgroundColor = Main_Color_Red;
        [_submitBtn setTitle:@"提 交" forState:UIControlStateNormal];
        
        [_submitBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
//            [selfp dismiss];
//            NSArray *ary = selfp.selectDic.allValues;
//            NSMutableString *resultReason = [[NSMutableString alloc] init];
//            for (NSInteger i = 0; i < ary.count; i++) {
//                NSInteger selectTag = [ary[i] integerValue];
//                if (selectTag == kSelectedTag) {
//                    NSString *re = selfp.selectDic.allKeys[i];
//                    [resultReason appendString:[NSString stringWithFormat:@"%@,", re]];
//                }
//            }
            if (selfp.selectSource.count > 0) {
                [selfp dismiss];
                NSString *resultReason = [selfp.selectSource componentsJoinedByString:@","];
                if (selfp.delegate && [selfp.delegate respondsToSelector:@selector(cancelOrderView:reason:)]) {
                    [selfp.delegate cancelOrderView:selfp reason:resultReason];
                }
                if (_callback) {
                    _callback(resultReason);
                }
            }else{
                [FTIndicator showToastMessage:@"请选择取消原因~"];
            }
        }];
    }
    return _submitBtn;
}

-(UITableView *)mTableView
{
    if (!_mTableView) {
        _mTableView = [[GCBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _mTableView.delegate = self;
        _mTableView.dataSource = self;
        _mTableView.separatorColor = [UIColor clearColor];
        _mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _mTableView;
}

-(UIControl *)overlayView
{
    if (!_overlayView) {
        _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        _overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [_overlayView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [_overlayView addSubview:self];
    }
    return _overlayView;
}

-(NSMutableArray *)selectSource
{
    if (!_selectSource) {
        _selectSource = [NSMutableArray new];
    }
    return _selectSource;
}

- (void)setTitleData:(NSArray *)ary {
    _titleAry = ary;
    [_mTableView reloadData];
}
-(void)setCallback:(GYCancelOrderAlertViewHandler)callback
{
    if (callback) {
        _callback = callback;
    }
}
#pragma mark - event
#pragma mark - method
- (void)initUI {
    self.backgroundColor = Main_Color_White;
    self.width = WF_UI_VIEW_WIDTH;
    self.height = WFCGFloatY(kGYCancelOrderAlertViewHeight);
    self.y = WF_UI_VIEW_HEIGHT + 64;
    
    [self addSubview:self.titleLab];
    [self addSubview:self.line];
    [self addSubview:self.subTitleLab];
    [self addSubview:self.mTableView];
    [self addSubview:self.submitBtn];
    
    if (!_selectDic) {
        _selectDic = [[NSMutableDictionary alloc] init];
    }
}

- (void)pop {
    [_selectDic removeAllObjects];
    [self.selectSource removeAllObjects];
    [_mTableView reloadData];
    
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self.overlayView];

    [UIView animateWithDuration:0.35 animations:^{
        self.center = CGPointMake(keywindow.bounds.size.width/2.0f, keywindow.bounds.size.height - self.frame.size.height/2);
    }];
}

- (void)dismiss {
    [self endEditing:YES];
    
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [UIView animateWithDuration:0.35 animations:^{
        self.center = CGPointMake(keywindow.bounds.size.width/2.0f, keywindow.bounds.size.height + self.frame.size.height / 2 + 20);
    }completion:^(BOOL finished) {
        [_overlayView removeFromSuperview];
    }];
}
#pragma mark - life
- (instancetype)init {
    self = [super init];
    if (self) {
        [self initUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _titleLab.frame = CGRectMake(10, 8, self.width - 20, 21);
    _line.frame = CGRectMake(0, 0, self.width, 1);
    _line.top = _titleLab.bottom + 5;
    
    _subTitleLab.frame = CGRectMake(10, 0, self.width - 20, 20);
    _subTitleLab.top = _line.bottom + 5;
    
    _mTableView.frame = CGRectMake(0, 0, self.width, self.height - 45);
    _mTableView.top = _subTitleLab.bottom + 5;
    
    _submitBtn.frame = CGRectMake(0, 0, self.width, 45);
    _submitBtn.top = self.height - 45;
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f, keywindow.bounds.size.height + self.frame.size.height / 2 + 20);
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 36;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYCancelOrderSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYCancelOrderSelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
//    cell.titleLab.text = _titleAry[indexPath.row];
//    NSInteger selectTag = [[_selectDic objectForKey:_titleAry[indexPath.row]] integerValue];
//    if (selectTag == kSelectedTag) {
//        cell.titleImgV.image = [UIImage imageNamed:@"符号-单选"];
//    } else {
//        cell.titleImgV.image = [UIImage imageNamed:@"单选"];
//    }
    
    NSString *str = _titleAry[indexPath.row];
    cell.titleLab.text = str;
    if ([self.selectSource containsObject:str]) {
        cell.titleImgV.image = [UIImage imageNamed:@"符号-单选"];
    }else{
        cell.titleImgV.image = [UIImage imageNamed:@"单选"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSInteger selectTag = [[_selectDic objectForKey:_titleAry[indexPath.row]] integerValue];
//    if (selectTag == kSelectedTag) {
//        [_selectDic setObject:[NSNumber numberWithInteger:kUnSelectedTag] forKey:_titleAry[indexPath.row]];
//    } else {
//        [_selectDic setObject:[NSNumber numberWithInteger:kSelectedTag] forKey:_titleAry[indexPath.row]];
//    }
    
    NSString *str = _titleAry[indexPath.row];
    if ([self.selectSource containsObject:str]) {
        [_selectSource removeObject:str];
    }else{
        [self.selectSource addObject:str];
    }
    
    [tableView reloadData];
}
@end
